package com.zkzx.operation.api.controller.truck;


import com.zkzx.common.company.model.CompanyVO;
import com.zkzx.common.global.Global;
import com.zkzx.common.truck.command.TruckCommand;
import com.zkzx.common.truck.model.TruckListVO;
import com.zkzx.common.truck.model.TruckVO;
import com.zkzx.common.truck.param.TruckFilterParam;
import com.zkzx.common.vo.PageResult;
import com.zkzx.common.vo.RequestFilterParams;
import com.zkzx.operation.api.client.company.CompanyClient;
import com.zkzx.operation.api.client.truck.TruckClient;
import com.zkzx.operation.api.controller.BaseController;
import com.zkzx.operation.api.service.truck.TruckService;
import com.zkzx.util.json.JSONResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;



/**
 * 北京中科智兴物联科技有限公司 @版权所有
 *
 * @Author: zhaoyihe@zkraising.com
 * @Date: 2021/05/11/21:00
 * @Description:
 */
@RestController
@Validated
@Api(tags="车辆管理")
@RequestMapping(value = "/trucks")
public class TruckController extends BaseController {

    @Autowired
    private TruckClient truckClient;

    @Autowired
    private TruckService truckService;

    @Autowired
    private CompanyClient companyClient;



    @GetMapping
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "获取车辆列表", notes = "获取车辆列表")
    public PageResult<TruckListVO> find(@ApiParam(value = "查询参数") TruckFilterParam filterParam,
                                        @ApiParam(value = "分页查询参数") RequestFilterParams params) {

        return truckClient.find(JSONResultUtil.object2JSON(filterParam),JSONResultUtil.object2JSON(params));
    }




    @GetMapping("/{id}")
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "获取车辆详细", notes = "获取车辆详细")
    public TruckVO getById(@PathVariable("id") Integer id){

        return truckService.getById(id, Global.ZERO,super.getUserId());
    }


    @PutMapping("/{id}")
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "修改车辆信息", notes = "修改车辆信息")
    public void update(@PathVariable("id") Integer id,@RequestBody @Valid TruckCommand command){

        // 修改基础信息
        truckClient.update(id,command,super.getUserId());
        command.setId(id);
        // 修改关系信息
        companyClient.updateTruck(command,Global.ZERO);

        // 获取网货提交所需要的信息 并提交到网货
        TruckVO truckVO = truckService.getById(id, Global.ZERO, super.getUserId());
        CompanyVO companyVO = companyClient.getById(truckVO.getCreateCompany());
        companyClient.verification(id,companyVO.getThreeId(),companyVO.getId(),Global.ZERO);
    }


}
